package com.budwk.app.sqfw.services.customservice.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.common.config.AppException;
import com.budwk.app.oss.services.SysOssService;
import com.budwk.app.sqfw.component.CommunityStreetComponent;
import com.budwk.app.sqfw.constants.CommonConstants;
import com.budwk.app.sqfw.dto.SqfwSummaryPdfExcelCollForAdminDTO;
import com.budwk.app.sqfw.enums.SupportBusinessTypeEnum;
import com.budwk.app.sqfw.models.*;
import com.budwk.app.sqfw.services.SqfwCompleteflowService;
import com.budwk.app.sqfw.services.SqfwFormSummaryPdfService;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfw.services.common.SqfwCustomForAdminCommonService;
import com.budwk.app.sqfw.vo.CheckDataForAdminVO;
import com.budwk.app.sqfw.vo.GenerateExcelUrlVO;
import com.budwk.app.sqfw.vo.GenerateWordAndPdfUrlVO;
import com.budwk.app.sqfw.vo.SqfwCustomForAdminCommonVO;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowPdfParamForController;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwV2.dto.dto.FireSafetyOnDayingRoadPdfDTO;
import com.budwk.app.sqfwV2.enums.CaringStaffEnum;
import com.budwk.app.sqfwV2.enums.EducationalInstitutionEnums;
import com.budwk.app.sqfwV2.enums.WasteCategoryFileServiceEnum;
import com.budwk.app.sqfwV2.enums.centuryCityOldCare.CenturyCityOldCareV2FileEnums;
import com.budwk.app.sqfwV2.enums.lanHuaDu.LanHuaDuFileServiceEnum;
import com.budwk.app.sqfwV2.service.wasteCategoryNew.WasteCategoryNewExcelService;
import com.budwk.app.sqfwV2.service.wasteCategoryNew.WasteCategoryNewPdfService;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysLogService;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.adminUtils.CommonCheckUtils;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Cnd;
import org.nutz.dao.entity.annotation.Table;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.dao.util.cri.Static;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

/**
 * @program: dfxy-community
 * @description: 垃圾分类巡查服务实现
 * @author: LJT
 * @create: 2024-07-09 10:57
 **/
@IocBean(name = "WasteCategoryForAdminService")
@Slf4j
public class WasteCategoryForAdminServiceImpl extends WasteCategoryNewExcelService implements SqfwCustomForAdminCommonService {
    @Inject
    private CommunityStreetComponent communityStreetComponent;
    @Inject
    private SqfwFormsummaryNewService sqfwFormsummaryNewService;
    @Inject
    private SqfwCompleteflowService sqfwCompleteflowService;
    @Inject
    private SysLogService sysLogService;
    @Inject
    private SysUserService sysUserService;
    @Inject
    private SqfwFormSummaryPdfService sqfwFormSummaryPdfService;
    @Inject
    private SysOssService sysOssService;
    @Inject
    private WasteCategoryNewPdfService wasteCategoryNewPdfService;

    @Override
    public Pagination list(SqfwCustomForAdminCommonVO bean) {
        if (StringUtils.isEmpty(bean.getType())) {
            throw new AppException(ResultCode.NOT_FOUND.getMsg());
        }
        long startTime = System.currentTimeMillis();
        Criteria criteria = buildQueryCond(bean);
        log.info("WasteCategoryForAdminServiceImpl-list-SQL: {}", criteria.toString());
        Pagination pagination = sqfwFormsummaryNewService.listPage(bean.getPageNo(), bean.getPageSize(), criteria);
        List<Sqfw_formSummary_new> sqfwCustomForAdminCommonVOList = pagination.getList(Sqfw_formSummary_new.class);
        if (ObjectUtil.isNotEmpty(sqfwCustomForAdminCommonVOList)) {
            List<SqfwCustomForAdminCommonVO> finalCommonVOList = new ArrayList<SqfwCustomForAdminCommonVO>();
            Map<String, String> pdfUrlMap = CommonCheckUtils.getPdfUrlByFlowId(sqfwCustomForAdminCommonVOList);
            sqfwCustomForAdminCommonVOList.forEach(source -> {
                SqfwCustomForAdminCommonVO target = new SqfwCustomForAdminCommonVO();
                BeanUtil.copyProperties(source, target);
                target.setPdfUrl(pdfUrlMap.get(target.getSqfwFlowId()));
                finalCommonVOList.add(target);
            });
            pagination.setList(finalCommonVOList);
        }
        log.info("WasteCategoryForAdminServiceImpl-list-耗时: {}ms", System.currentTimeMillis() - startTime);
        return pagination;
    }

    @Override
    @Aop(TransAop.READ_COMMITTED)
    public int delete(String id) {
        Sqfw_formSummary_new sqfwFormSummaryNew = sqfwFormsummaryNewService.fetch(id);
        if (sqfwFormSummaryNew == null) {
            throw new AppException(ResultCode.NULL_DATA_ERROR.getMsg());
        }
        batchDeleteDataNew(SecurityUtil.getUserId(), sqfwFormSummaryNew);
        return 0;
    }

    @Override
    @Aop(TransAop.READ_COMMITTED)
    public int batchDelete(List<String> ids) {
        if (ids == null || ids.size() == 0) {
            throw new AppException(ResultCode.NULL_DATA_ERROR.getMsg());
        }
        // 查询数据
        Cnd cnd = Cnd.NEW();
        cnd.and(Sqfw_formSummary::getId, "in", ids);
        List<Sqfw_formSummary_new> newList = sqfwFormsummaryNewService.query(cnd);
        if (newList.isEmpty()) {
            throw new AppException(ResultCode.NULL_DATA_ERROR.getMsg());
        }
        String userId = SecurityUtil.getUserId();
        // 异步执行删除操作
        if (!newList.isEmpty()) {
            ExecutorService executorService = Executors.newSingleThreadExecutor();
            CompletableFuture.runAsync(() -> {
                for (Sqfw_formSummary_new sqfwFormSummaryNew : newList) {
                    batchDeleteDataNew(userId, sqfwFormSummaryNew);
                }
            }, executorService);
            executorService.shutdown();
        }
        return newList.size();
    }

    @Override
    public int update(Sqfw_formSummary_new sqfwFormsummaryNew) {
        return 0;
    }

    @Override
    public List<GenerateExcelUrlVO> downloadExcel(List<ExcelCreateConfig> excelConfigList, SqfwCustomForAdminCommonVO bean) {
        if (org.apache.commons.lang.StringUtils.isEmpty(bean.getType())) {
            throw new AppException(ResultCode.NOT_FOUND.getMsg());
        }

        // 如果formSummaryNewIdList为空，根据条件下载，否则根据勾选id下载
        if (ObjectUtil.isEmpty(bean.getFormSummaryNewIdList()) || bean.getFormSummaryNewIdList().size() <= 0) {
            Criteria criteria = buildQueryCond(bean);
            List<Sqfw_formSummary_new> summaryNewList = sqfwFormsummaryNewService.query(criteria);
            if (summaryNewList.size() > 0) {
                bean.setFormSummaryNewIdList(sqfwFormsummaryNewService.query(criteria).stream().map(Sqfw_formSummary_new::getId).collect(Collectors.toList()));
            } else {
                return new ArrayList<GenerateExcelUrlVO>();
            }
        } else {
            Cnd cnd = Cnd.NEW();
            // 时间
            if (ObjectUtil.isNotEmpty(bean.getQueryStartTime()) && ObjectUtil.isNotEmpty(bean.getQueryEndTime())) {
                cnd.and(Sqfw_formSummary_new::getFillDate, ">=", communityStreetComponent.handleStartQueryTime(bean.getQueryStartTime()));
                cnd.and(Sqfw_formSummary_new::getFillDate, "<=", communityStreetComponent.handleEndQueryTime(bean.getQueryEndTime()));
            }
            cnd.and(Sqfw_formSummary_new::getId, "IN", bean.getFormSummaryNewIdList());
            List<Sqfw_formSummary_new> summaryNewList = sqfwFormsummaryNewService.query(cnd);
            if (summaryNewList.size() > 0) {
                bean.setFormSummaryNewIdList(summaryNewList.stream().map(Sqfw_formSummary_new::getId).collect(Collectors.toList()));
            } else {
                return new ArrayList<GenerateExcelUrlVO>();
            }
        }
        if (excelConfigList.get(0).getOnlyGenerateTemplateFlag()) {
            bean.setFormSummaryNewIdList(Collections.emptyList());
        }
        return super.generateExcelForAdmin(excelConfigList, SecurityUtil.getUserId(), bean.getFormSummaryNewIdList());
    }

    @Override
    public GenerateWordAndPdfUrlVO downloadPdf
            (List<ExcelCreateConfig> excelConfigList, SqfwCustomForAdminCommonVO bean) {
        if (StringUtils.isEmpty(bean.getType())) {
            throw new AppException(ResultCode.NOT_FOUND.getMsg());
        }
        GenerateWordAndPdfUrlVO generateWordAndPdfUrlVO = new GenerateWordAndPdfUrlVO();
        if (excelConfigList.get(0).getOnlyGenerateTemplateFlag()) {
            WasteCategoryFileServiceEnum wasteCategoryFileServiceEnum = WasteCategoryFileServiceEnum.getEnums(excelConfigList.get(0).getFileIdentity());
            String filePath = wasteCategoryFileServiceEnum.getFileTemplatePath();
            String fileName = wasteCategoryFileServiceEnum.getFileName() + ".docx";
            generateWordAndPdfUrlVO.setPdfUrl(sysOssService.upload(CommonFormOfficeUtils.getTempPdfFile(filePath), fileName).getUrl());
            return generateWordAndPdfUrlVO;
        }
        // 如果formSummaryNewIdList为空，根据条件下载，否则根据勾勾选id下载
        if (ObjectUtil.isEmpty(bean.getFormSummaryNewIdList()) || bean.getFormSummaryNewIdList().size() <= 0) {
            Criteria criteria = buildQueryCond(bean);
            List<Sqfw_formSummary_new> summaryNewList = sqfwFormsummaryNewService.query(criteria);
            if (summaryNewList.size() > 0) {
                bean.setFormSummaryNewIdList(summaryNewList.stream().map(Sqfw_formSummary_new::getId).collect(Collectors.toList()));
            } else {
                return new GenerateWordAndPdfUrlVO();
            }
        }
        List<SqfwSummaryPdfExcelCollForAdminDTO> summaryIdServiceList = new ArrayList<SqfwSummaryPdfExcelCollForAdminDTO>();
        for (String id : bean.getFormSummaryNewIdList()) {
            SqfwSummaryPdfExcelCollForAdminDTO sqfwSummaryPdfExcelCollForAdminDTO = new SqfwSummaryPdfExcelCollForAdminDTO();
            sqfwSummaryPdfExcelCollForAdminDTO.setSumId(id);
            sqfwSummaryPdfExcelCollForAdminDTO.setServiceType(bean.getType());
            summaryIdServiceList.add(sqfwSummaryPdfExcelCollForAdminDTO);
        }
        generateWordAndPdfUrlVO.setPdfUrl(generatePdfColl(bean.getFormSummaryNewIdList(), excelConfigList.get(0).getFileIdentity()));
        return generateWordAndPdfUrlVO;
    }

    @Override
    public List<GenerateExcelUrlVO> downloadPdfList
            (List<ExcelCreateConfig> excelConfigList, SqfwCustomForAdminCommonVO bean) {
        return null;
    }

    @Override
    public Criteria buildQueryCond(SqfwCustomForAdminCommonVO bean) {
        Criteria cri = Cnd.cri();
        SqlExpressionGroup criSql = cri.where();
        if (StringUtils.isEmpty(bean.getType())) {
            throw new AppException(ResultCode.NOT_FOUND.getMsg());
        }
        // 组装查询条件
        if (StringUtils.isNotBlank(bean.getPageOrderName()) && Strings.isNotBlank(bean.getPageOrderBy())) {
            if (bean.getPageOrderBy().equals(CommonConstants.ASC)) {
                cri.getOrderBy().asc(bean.getPageOrderName());
            }
            if (bean.getPageOrderBy().equals(CommonConstants.DESC)) {
                cri.getOrderBy().desc(bean.getPageOrderName());
            }
        } else {
            cri.getOrderBy().desc(Sqfw_formSummary_new::getFillDate);
        }
        // 类型
        criSql.andEquals(SqfwCustomForAdminCommonVO::getType, bean.getType());

        Sys_user sysUser = sysUserService.fetch(SecurityUtil.getUserId());
        if (ObjectUtils.isEmpty(sysUser)) {
            throw new AppException(ResultCode.USER_NOT_FOUND.getMsg());
        }
        // 区域5级检查
        CommonCheckUtils.checkAreaRange(criSql);
        // 省
        if (StringUtils.isNotBlank(bean.getProvince()) && !CommonCheckUtils.ZHISHU.equals(bean.getProvince())) {
            criSql.andEquals(SqfwCustomForAdminCommonVO::getProvince, bean.getProvince());
        }
        // 市
        if (StringUtils.isNotBlank(bean.getCity()) && !CommonCheckUtils.ZHISHU.equals(bean.getCity())) {
            criSql.andEquals(SqfwCustomForAdminCommonVO::getCity, bean.getCity());
        }
        // 区
        if (StringUtils.isNotBlank(bean.getDistrict()) && !CommonCheckUtils.ZHISHU.equals(bean.getDistrict())) {
            criSql.andEquals(SqfwCustomForAdminCommonVO::getDistrict, bean.getDistrict());
        }
        // 街道
        if (StringUtils.isNotBlank(bean.getCounty()) && !CommonCheckUtils.ZHISHU.equals(bean.getCounty())) {
            criSql.andEquals(SqfwCustomForAdminCommonVO::getCounty, bean.getCounty());
        }
        // 时间
        if (ObjectUtil.isNotEmpty(bean.getQueryStartTime()) && ObjectUtil.isNotEmpty(bean.getQueryEndTime())) {
            criSql.andBetween(Sqfw_formSummary_new::getFillDate,
                    communityStreetComponent.handleStartQueryTime(bean.getQueryStartTime()),
                    communityStreetComponent.handleEndQueryTime(bean.getQueryEndTime()));
        }
        // 辖区
        if (StringUtils.isNotBlank(bean.getCommunity()) && !CommonCheckUtils.ZHISHU.equals(bean.getCommunity())) {
            criSql.andEquals(Sqfw_formSummary_new::getCommunity, bean.getCommunity());
        }
        // 全局搜索
        if (StringUtils.isNotEmpty(bean.getQueryAll())) {
            SqlExpressionGroup rectificationMeasure = Cnd.exps(new Static("param_data->>'rectificationMeasure' like '%" + bean.getQueryAll() + "%'"));
            SqlExpressionGroup address = Cnd.exps(new Static("param_data->>'address' like '%" + bean.getQueryAll() + "%'"));
            SqlExpressionGroup problems = Cnd.exps(new Static("param_data->>'problems' like '%" + bean.getQueryAll() + "%'"));
            SqlExpressionGroup rectificationType = Cnd.exps(new Static("param_data->>'rectificationType' like '%" + bean.getQueryAll() + "%'"));
            SqlExpressionGroup county = Cnd.exps(new Static("county like '%" + bean.getQueryAll() + "%'"));
            SqlExpressionGroup community = Cnd.exps(new Static("community like '%" + bean.getQueryAll() + "%'"));
            SqlExpressionGroup createUserName = Cnd.exps(new Static("create_user_name like '%" + bean.getQueryAll() + "%'"));
            criSql.and(rectificationMeasure.or(address).or(problems).or(rectificationType).or(county).or(community).or(createUserName));
        }
        // 需要排除的数据
        criSql.andNotEquals(Sqfw_formSummary_new::getFillDate, CommonConstants.WASTE_CREATE_TIME);
        // 创建人
        if (StringUtils.isNotBlank(bean.getCreateUserName()) && !CommonCheckUtils.ZHISHU.equals(bean.getCreateUserName())) {
            criSql.andEquals(Sqfw_formSummary_new::getCreateUserName, bean.getCreateUserName());
        }
        // 联系地址
        if (StringUtils.isNotEmpty(bean.getAddress())) {
            criSql.andEquals("param_data->>'address'", bean.getAddress());
        }
        // 措施
        if (StringUtils.isNotEmpty(bean.getRectificationMeasure())) {
            criSql.andEquals("param_data->>'rectificationMeasure'", bean.getRectificationMeasure());
        }
        // 问题
        if (StringUtils.isNotEmpty(bean.getProblems())) {
            if (bean.getProblems().contains("、")) {
                String[] problems = bean.getProblems().split("、");
                StringBuffer sb = new StringBuffer();
                for (String problem : problems) {
                    sb.append(" (param_data->>'problems' LIKE '%").append(problem).append("%') ").append(" OR");
                }
                if (ObjectUtil.isNotEmpty(sb) && sb.toString().endsWith("OR")) {
                    criSql.and(new Static(" (" + sb.substring(0, sb.length() - 2) + ")"));
                }
            } else {
                criSql.andLike("param_data->>'problems'", bean.getProblems());
            }
        }
        // 状态
        if (StringUtils.isNotEmpty(bean.getRectificationType())) {
            criSql.andEquals("param_data->>'rectificationType'", bean.getRectificationType());
        }
        return cri;
    }

    @Override
    public List<CheckDataForAdminVO> checkDataInfoList(SqfwCustomForAdminCommonVO bean) {
        List<CheckDataForAdminVO> checkDataForAdminVOList = new ArrayList<CheckDataForAdminVO>();
        for (WasteCategoryFileServiceEnum item : WasteCategoryFileServiceEnum.values()) {
            if ("waste_category_rec_excel,waste_category_rec_doc".contains(item.getCode())) {
                CheckDataForAdminVO checkDataForAdminVO = new CheckDataForAdminVO();
                checkDataForAdminVO.setFileIdentity(item.getCode());
                checkDataForAdminVO.setCreateConditionFlag(true);
                checkDataForAdminVO.setFileName(item.getFileName());
                if (item.getFilePath().endsWith("docx")) {
                    checkDataForAdminVO.setFileName(checkDataForAdminVO.getFileName() + ".pdf");
                } else {
                    checkDataForAdminVO.setFileName(checkDataForAdminVO.getFileName() + ".xlsx");
                }
                checkDataForAdminVOList.add(checkDataForAdminVO);
            }
        }
        return checkDataForAdminVOList;
    }

    @Override
    public void reGeneratePdf(String id) {
        CompleteFlowPdfParamForController param = new CompleteFlowPdfParamForController();
        param.setCompleteFlowId(id);
        wasteCategoryNewPdfService.reCreatePdfAndMsg(param);
    }

    /**
     * 生成pdf合集
     *
     * @return
     */
    private String generatePdfColl(List<String> ids, String code) {
        String url = "";
        List<Sqfw_formSummary_new> formSummaryList = sqfwFormsummaryNewService.query(Cnd.where(Sqfw_formSummary_new::getId, "IN", ids));
        if (CollectionUtil.isNotEmpty(formSummaryList)) {
            List<String> summaryIdS = formSummaryList.stream().map(Sqfw_formSummary_new::getSqfwFlowId).collect(Collectors.toList());
            List<Sqfw_completeflow> completeflow = sqfwCompleteflowService.query(Cnd.where(Sqfw_completeflow::getId, "IN", summaryIdS)
                    .and(Sqfw_completeflow::getResourcePath, "!=", "creating")
                    .and(Sqfw_completeflow::getResourcePath, "!=", "error"));
            if (CollectionUtil.isNotEmpty(completeflow)) {
                List<String> pdfPathList = completeflow.stream().map(Sqfw_completeflow::getResourcePath).collect(Collectors.toList());
                url = CommonFormOfficeUtils.createConvergePdfAndUpload(pdfPathList, WasteCategoryFileServiceEnum.getFileName(code));
            }
        }
        return url;
    }

    /**
     * 批量删除
     *
     * @param userId
     * @param data
     */
    private void batchDeleteDataNew(String userId, Sqfw_formSummary_new data) {
        //1 记录日志
        sysLogService.insertDelLog(userId, JSONObject.toJSONString(data), Sqfw_cenSusPaper.class.getAnnotation(Table.class).value(),
                "com.budwk.app.sqfw.services.customservice.DaYingLuSecurityForAdminServiceImpl.batchDeleteDataNew",
                "com.budwk.app.sqfw.controllers.admin.DaYingLuSecurityForAdminController.batchDelete");
        // 2 删除数据
        sqfwCompleteflowService.deleteAndUpdateWO(data.getSqfwFlowId());
    }

    /**
     * 处理返回的list数据
     *
     * @param source
     * @param pdfUrlMap
     * @param dictCodesMap
     * @param finalCommonVOList
     */
    private void processListData(
            Sqfw_formSummary_new source,
            Map<String, String> pdfUrlMap,
            Map<String, String> dictCodesMap,
            List<SqfwCustomForAdminCommonVO> finalCommonVOList) {
        SqfwCustomForAdminCommonVO target = new SqfwCustomForAdminCommonVO();
        BeanUtil.copyProperties(source, target);
        target.setPdfUrl(pdfUrlMap.get(target.getSqfwFlowId()));
        FireSafetyOnDayingRoadPdfDTO FireSafetyDTO = JSONObject.parseObject(target.getParamData(), FireSafetyOnDayingRoadPdfDTO.class);
        if (ObjectUtil.isNotEmpty(FireSafetyDTO)) {
            String mainBusinessScope = FireSafetyDTO.getMainBusinessScope();
            if (StringUtils.isNotEmpty(mainBusinessScope)) {
                List<String> mainBusinessScopes = Arrays.asList(mainBusinessScope.split(","));
                StringBuffer sb = new StringBuffer();
                mainBusinessScopes.forEach(mainBusiness -> {
                    String finalMainBusiness = dictCodesMap.get(mainBusiness);
                    if (StringUtils.isNotEmpty(finalMainBusiness)) {
                        sb.append(finalMainBusiness).append(",");
                    } else {
                        sb.append(mainBusinessScope).append(",");
                    }
                });
                target.setMainBusinessScopeName(sb.substring(0, sb.length() - 1));
            }
            String usageFireElectrcity = FireSafetyDTO.getFireAndElectricityUsage();
            if (StringUtils.isNotEmpty(usageFireElectrcity)) {
                List<String> usageFireElectrcitys = Arrays.asList(usageFireElectrcity.split(","));
                StringBuffer sb = new StringBuffer();
                usageFireElectrcitys.forEach(mainBusiness -> {
                    String finalMainBusiness = dictCodesMap.get(mainBusiness);
                    if (StringUtils.isNotEmpty(finalMainBusiness)) {
                        sb.append(finalMainBusiness).append(",");
                    } else {
                        sb.append(mainBusinessScope).append(",");
                    }
                });
                target.setFireAndElectricityUsageName(sb.substring(0, sb.length() - 1));
            }
            if (ObjectUtil.isNotEmpty(FireSafetyDTO.getAccommodatePeople())) {
                String rectificateType = FireSafetyDTO.getAccommodatePeople().getResidentialType();
                if (StringUtils.isNotEmpty(rectificateType)) {
                    List<String> rectificateTypes = Arrays.asList(rectificateType.split(","));
                    StringBuffer sb = new StringBuffer();
                    rectificateTypes.forEach(mainBusiness -> {
                        String finalMainBusiness = dictCodesMap.get(mainBusiness);
                        if (StringUtils.isNotEmpty(finalMainBusiness)) {
                            sb.append(finalMainBusiness).append(",");
                        } else {
                            sb.append(mainBusinessScope).append(",");
                        }

                    });
                    target.setRectificateTypeName(sb.substring(0, sb.length() - 1));
                }
            }
        }
        finalCommonVOList.add(target);
    }

    /**
     * 检查是否满足下载条件
     *
     * @param code
     * @param paramData
     * @return
     */
    private Boolean checkPdfDownloadCond(String code, String paramData) {
        if (StringUtils.isEmpty(paramData)) {
            return false;
        }
        /**
         * lhdSafetyHazardDocGenerator lhdSafetyHazardDocGenerator = new lhdSafetyHazardDocGenerator();
         * lhdRecReDocGenerator lhdRecReDocGenerator = new lhdRecReDocGenerator();
         *  lhdOrderLetterDocGenerator = new lhdOrderLetterDocGenerator();
         */
        FireSafetyOnDayingRoadPdfDTO roadPdfDTO = JSONUtil.toBean(paramData, FireSafetyOnDayingRoadPdfDTO.class);
        if (LanHuaDuFileServiceEnum.D.getCode().equals(code)) {
            // 判断场所面积
            String placeArea = roadPdfDTO.getPlaceArea();
            if (StringUtils.isEmpty(placeArea)) {
                return false;
            }
            double pArea = Double.parseDouble(placeArea);
            if (pArea <= 300) {
                return false;
            }
            // “整改时限”为“限期整改
            if (roadPdfDTO.getIsRectified() == 3) {
                return true;
            }
        } else if (LanHuaDuFileServiceEnum.B.getCode().equals(code)) {
            // 初始数据 没有复查数据
            if (StringUtils.isEmpty(roadPdfDTO.getLastSummaryId())) {
                return false;
            }
            double pArea = Double.parseDouble(roadPdfDTO.getPlaceArea());
            if (pArea <= 300) {
                return false;
            }
            // 检查上次的数据是否存在期限整改
            Sqfw_formSummary_new lastData = sqfwFormsummaryNewService.fetch(roadPdfDTO.getLastSummaryId());
            FireSafetyOnDayingRoadPdfDTO lastPdfDto = JSONObject.parseObject(lastData.getParamData(), FireSafetyOnDayingRoadPdfDTO.class);
            Integer isRectified = lastPdfDto.getIsRectified();
            if (isRectified == 3 && roadPdfDTO.getIsRectified() == 3) {
                // 上一次的数据存在期限整改 本次数据存在期限整改
                return true;
            }
        } else if (LanHuaDuFileServiceEnum.E.getCode().equals(code)) {
            // 初始数据 没有复查数据
            if (StringUtils.isEmpty(roadPdfDTO.getLastSummaryId())) {
                return false;
            }
            double pArea = Double.parseDouble(roadPdfDTO.getPlaceArea());
            if (pArea <= 300) {
                return false;
            }
            // 检查上次的数据是否存在期限整改
            Sqfw_formSummary_new lastData = sqfwFormsummaryNewService.fetch(roadPdfDTO.getLastSummaryId());
            FireSafetyOnDayingRoadPdfDTO lastPdfDto = JSONObject.parseObject(lastData.getParamData(), FireSafetyOnDayingRoadPdfDTO.class);
            Integer isRectified = lastPdfDto.getIsRectified();
            if (isRectified == 3) {
                // 上一次的数据存在期限整改
                return true;
            }
        } else if (LanHuaDuFileServiceEnum.C.getCode().equals(code)) {
            return true;
        }
        return false;
    }
}